<template>
	<view>
		<!-- 用户信息 -->
		<view class="uni-list userInfo">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover">
				<view class="uni-list-cell-navigate uni-navigate-right uni-media-list" v-if="userInfo">
					<view class="uni-media-list-logo">
						<image v-if="userInfo.avatar" :src="userInfo.avatar"></image>
						<image v-else :src="img"></image>
					</view>
					<view class="uni-media-list-body">
						<view v-if="userInfo">
							<view class="uni-media-list-text-top">{{userInfo.user_name}}</view>
							<view class="uni-media-list-text-bottom uni-ellipsis">{{userInfo.user_group_name}}</view>
						</view>
					</view>
				</view>
				<view class="uni-list-cell-navigate uni-navigate-right uni-media-list" v-else>
					<view class="uni-media-list-logo">
						<image :src="img"></image>
					</view>
					<view class="uni-media-list-body">
						<view class="uni-media-list-text-top" @click="toLogin()">登录</view>
						<view class="uni-media-list-text-bottom uni-ellipsis" @click="toReg()">注册</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 用户操作 -->
		<view class="uni-list">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover">
				<view class="uni-list-cell-navigate uni-navigate-right">
					我的订单
				</view>
			</view>
			<view class="uni-list-cell" hover-class="uni-list-cell-hover">
				<view class="uni-list-cell-navigate uni-navigate-right">
					我的收藏
				</view>
			</view>
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" @tap="toAddress()">
				<view class="uni-list-cell-navigate uni-navigate-right">
					收货地址
				</view>
			</view>
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" @tap="toUpdatePwd()">
				<view class="uni-list-cell-navigate uni-navigate-right">
					密码更改
				</view>
			</view>
			
		</view>
		
		<!-- 退出登录 -->
		<view class="logout" @tap="logout()">
			<button type="primary">退出登录</button>
		</view>
	</view>

</template>

<script>
	import {
		getUserInfo,
		loginOut
	} from '../../../common/api.js'
	import {
		showError,
		showSuccess
	} from '../../../common/fast.js'
	export default {
		data() {
			return {
				userInfo: {},
				img: 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/images/shuijiao.jpg?imageView2/3/w/200/h/100/q/90',
			}
		},
		methods: {
			toLogin() {
				console.log('login')
				uni.navigateTo({
					url: '../../user/login'
				})
			},
			toReg() {
				console.log('reg')
				uni.navigateTo({
					url: '../../user/reg'
				})
			},
			toAddress() {
				console.log('toAddress')
				uni.navigateTo({
					url: '../../address/address'
				})
			},
			handleUserInfo() {
				let reqData = {
					user_token: this.$store.state.userInfo.user_token
				}
				getUserInfo(reqData).then(res => {
					this.userInfo = res.data.result
				})
			},
			toUpdatePwd() {
				uni.navigateTo({
					url: '../../user/pwd_update'
				})
			},
			logout(){
				let reqData = {
					user_token:  this.$store.state.userInfo.user_token
				}
				loginOut(reqData).then( res => {
					console.log(res.data)
					let resData = res.data
					if (resData.status === 'success'){
						showSuccess('退出成功')
						this.handleUserInfo()
					}
				})
			}
		},
		onLoad() {


		},
		onShow() {
			this.handleUserInfo()
		}

	}
</script>

<style>
	@import "../../../common/uni.css";

	.logout {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 50px;
	}
</style>
